home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_061 / microemacs / display.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  24KB  |  1,090 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include        <stdio.h>
  10. #include    "estruct.h"
  11. #include        "edef.h"
  12.  
  13. #if    MEGAMAX & ST520
  14. overlay    "display"
  15. #endif
  16.  
  17. typedef struct  VIDEO {
  18.         int    v_flag;                 /* Flags */
  19. #if    COLOR
  20.     int    v_fcolor;        /* current forground color */
  21.     int    v_bcolor;        /* current background color */
  22.     int    v_rfcolor;        /* requested forground color */
  23.     int    v_rbcolor;        /* requested background color */
  24. #endif
  25.         char    v_text[1];              /* Screen data. */
  26. }       VIDEO;
  27.  
  28. #define VFCHG   0x0001                  /* Changed flag            */
  29. #define    VFEXT    0x0002            /* extended (beyond column 80)    */
  30. #define    VFREV    0x0004            /* reverse video status        */
  31. #define    VFREQ    0x0008            /* reverse video request    */
  32. #define    VFCOL    0x0010            /* color change requested    */
  33.  
  34. VIDEO   **vscreen;                      /* Virtual screen. */
  35. #if    MEMMAP == 0
  36. VIDEO   **pscreen;                      /* Physical screen. */
  37. #endif
  38.  
  39. /*
  40.  * Initialize the data structures used by the display code. The edge vectors
  41.  * used to access the screens are set up. The operating system's terminal I/O
  42.  * channel is set up. All the other things get initialized at compile time.
  43.  * The original window has "WFCHG" set, so that it will get completely
  44.  * redrawn on the first call to "update".
  45.  */
  46. vtinit()
  47. {
  48.     register int i;
  49.     register VIDEO *vp;
  50.     char *malloc();
  51.  
  52.     TTopen();        /* open the screen */
  53.     TTkopen();        /* open the keyboard */
  54.     TTrev(FALSE);
  55.     vscreen = (VIDEO **) malloc(term.t_nrow*sizeof(VIDEO *));
  56.  
  57.     if (vscreen == NULL)
  58.         exit(1);
  59.  
  60. #if    MEMMAP == 0
  61.     pscreen = (VIDEO **) malloc(term.t_nrow*sizeof(VIDEO *));
  62.  
  63.     if (pscreen == NULL)
  64.         exit(1);
  65. #endif
  66.  
  67.     for (i = 0; i < term.t_nrow; ++i)
  68.         {
  69.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  70.  
  71.         if (vp == NULL)
  72.             exit(1);
  73.  
  74.     vp->v_flag = 0;
  75. #if    COLOR
  76.     vp->v_rfcolor = 7;
  77.     vp->v_rbcolor = 0;
  78. #endif
  79.         vscreen[i] = vp;
  80. #if    MEMMAP == 0
  81.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  82.  
  83.         if (vp == NULL)
  84.             exit(1);
  85.  
  86.     vp->v_flag = 0;
  87.         pscreen[i] = vp;
  88. #endif
  89.         }
  90. }
  91.  
  92. /*
  93.  * Clean up the virtual terminal system, in anticipation for a return to the
  94.  * operating system. Move down to the last line and clear it out (the next
  95.  * system prompt will be written in the line). Shut down the channel to the
  96.  * terminal.
  97.  */
  98. vttidy()
  99. {
  100.     mlerase();
  101.     movecursor(term.t_nrow, 0);
  102.     TTflush();
  103.     TTclose();
  104.     TTkclose();
  105. }
  106.  
  107. /*
  108.  * Set the virtual cursor to the specified row and column on the virtual
  109.  * screen. There is no checking for nonsense values; this might be a good
  110.  * idea during the early stages.
  111.  */
  112. vtmove(row, col)
  113. {
  114.     vtrow = row;
  115.     vtcol = col;
  116. }
  117.  
  118. /* Write a character to the virtual screen. The virtual row and
  119.    column are updated. If we are not yet on left edge, don't print
  120.    it yet. If the line is too long put a "$" in the last column.
  121.    This routine only puts printing characters into the virtual
  122.    terminal buffers. Only column overflow is checked.
  123. */
  124.  
  125. vtputc(c)
  126.  
  127. int c;
  128.  
  129. {
  130.     register VIDEO *vp;    /* ptr to line being updated */
  131.  
  132.     vp = vscreen[vtrow];
  133.  
  134.     if (c == '\t') {
  135.         do {
  136.             vtputc(' ');
  137.         } while (((vtcol + taboff)&0x07) != 0);
  138.     } else if (vtcol >= term.t_ncol) {
  139.         ++vtcol;
  140.         vp->v_text[term.t_ncol - 1] = '$';
  141.     } else if (c < 0x20 || c == 0x7F) {
  142.         vtputc('^');
  143.         vtputc(c ^ 0x40);
  144.     } else {
  145.         if (vtcol >= 0)
  146.             vp->v_text[vtcol] = c;
  147.         ++vtcol;
  148.     }
  149. }
  150.  
  151. /*
  152.  * Erase from the end of the software cursor to the end of the line on which
  153.  * the software cursor is located.
  154.  */
  155. vteeol()
  156. {
  157.     register VIDEO      *vp;
  158.  
  159.     vp = vscreen[vtrow];
  160.     while (vtcol < term.t_ncol)
  161.         vp->v_text[vtcol++] = ' ';
  162. }
  163.  
  164. /* upscreen:    user routine to force a screen update
  165.         always finishes complete update        */
  166.  
  167. upscreen(f, n)
  168.  
  169. {
  170.     update(TRUE);
  171.     return(TRUE);
  172. }
  173.  
  174. /*
  175.  * Make sure that the display is right. This is a three part process. First,
  176.  * scan through all of the windows looking for dirty ones. Check the framing,
  177.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  178.  * correct for the current window. Third, make the virtual and physical
  179.  * screens the same.
  180.  */
  181. update(force)
  182.  
  183. int force;    /* force update past type ahead? */
  184.  
  185. {
  186.     register WINDOW *wp;
  187.  
  188. #if    TYPEAH
  189.     if (force == FALSE && typahead())
  190.         return(TRUE);
  191. #endif
  192. #if    VISMAC == 0
  193.     if (force == FALSE && kbdmode == PLAY)
  194.         return(TRUE);
  195. #endif
  196.  
  197.     /* update any windows that need refreshing */
  198.     wp = wheadp;
  199.     while (wp != NULL) {
  200.         if (wp->w_flag) {
  201.             /* if the window has changed, service it */
  202.             reframe(wp);    /* check the framing */
  203.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  204.                 updone(wp);    /* update EDITed line */
  205.             else if (wp->w_flag & ~WFMOVE)
  206.                 updall(wp);    /* update all lines */
  207.             if (wp->w_flag & WFMODE)
  208.                 modeline(wp);    /* update modeline */
  209.             wp->w_flag = 0;
  210.             wp->w_force = 0;
  211.         }
  212.         /* on to the next window */
  213.         wp = wp->w_wndp;
  214.     }
  215.  
  216.     /* recalc the current hardware cursor location */
  217.     updpos();
  218.  
  219. #if    MEMMAP
  220.     /* update the cursor and flush the buffers */
  221.     movecursor(currow, curcol - lbound);
  222. #endif
  223.  
  224.     /* check for lines to de-extend */
  225.     upddex();
  226.  
  227.     /* if screen is garbage, re-plot it */
  228.     if (sgarbf != FALSE)
  229.         updgar();
  230.  
  231.     /* update the virtual screen to the physical screen */
  232.     updupd(force);
  233.  
  234.     /* update the cursor and flush the buffers */
  235.     movecursor(currow, curcol - lbound);
  236.     TTflush();
  237.     return(TRUE);
  238. }
  239.  
  240. /*    reframe:    check to see if the cursor is on in the window
  241.             and re-frame it if needed or wanted        */
  242.  
  243. reframe(wp)
  244.  
  245. WINDOW *wp;
  246.  
  247. {
  248.     register LINE *lp;
  249.     register int i;
  250.  
  251.     /* if not a requested reframe, check for a needed one */
  252.     if ((wp->w_flag & WFFORCE) == 0) {
  253.         lp = wp->w_linep;
  254.         for (i = 0; i < wp->w_ntrows; i++) {
  255.  
  256.             /* if the line is in the window, no reframe */
  257.             if (lp == wp->w_dotp)
  258.                 return(TRUE);
  259.  
  260.             /* if we are at the end of the file, reframe */
  261.             if (lp == wp->w_bufp->b_linep)
  262.                 break;
  263.  
  264.             /* on to the next line */
  265.             lp = lforw(lp);
  266.         }
  267.     }
  268.  
  269.     /* reaching here, we need a window refresh */
  270.     i = wp->w_force;
  271.  
  272.     /* how far back to reframe? */
  273.     if (i > 0) {        /* only one screen worth of lines max */
  274.         if (--i >= wp->w_ntrows)
  275.             i = wp->w_ntrows - 1;
  276.     } else if (i < 0) {    /* negative update???? */
  277.         i += wp->w_ntrows;
  278.         if (i < 0)
  279.             i = 0;
  280.     } else
  281.         i = wp->w_ntrows / 2;
  282.  
  283.     /* backup to new line at top of window */
  284.     lp = wp->w_dotp;
  285.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  286.         --i;
  287.         lp = lback(lp);
  288.     }
  289.  
  290.     /* and reset the current line at top of window */
  291.     wp->w_linep = lp;
  292.     wp->w_flag |= WFHARD;
  293.     wp->w_flag &= ~WFFORCE;
  294.     return(TRUE);
  295. }
  296.  
  297. /*    updone:    update the current line    to the virtual screen        */
  298.  
  299. updone(wp)
  300.  
  301. WINDOW *wp;    /* window to update current line in */
  302.  
  303. {
  304.     register LINE *lp;    /* line to update */
  305.     register int sline;    /* physical screen line to update */
  306.     register int i;
  307.  
  308.     /* search down the line we want */
  309.     lp = wp->w_linep;
  310.     sline = wp->w_toprow;
  311.     while (lp != wp->w_dotp) {
  312.         ++sline;
  313.         lp = lforw(lp);
  314.     }
  315.  
  316.     /* and update the virtual line */
  317.     vscreen[sline]->v_flag |= VFCHG;
  318.     vscreen[sline]->v_flag &= ~VFREQ;
  319.     vtmove(sline, 0);
  320.     for (i=0; i < llength(lp); ++i)
  321.         vtputc(lgetc(lp, i));
  322. #if    COLOR
  323.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  324.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  325. #endif
  326.     vteeol();
  327. }
  328.  
  329. /*    updall:    update all the lines in a window on the virtual screen */
  330.  
  331. updall(wp)
  332.  
  333. WINDOW *wp;    /* window to update lines in */
  334.  
  335. {
  336.     register LINE *lp;    /* line to update */
  337.     register int sline;    /* physical screen line to update */
  338.     register int i;
  339.  
  340.     /* search down the lines, updating them */
  341.     lp = wp->w_linep;
  342.     sline = wp->w_toprow;
  343.     while (sline < wp->w_toprow + wp->w_ntrows) {
  344.  
  345.         /* and update the virtual line */
  346.         vscreen[sline]->v_flag |= VFCHG;
  347.         vscreen[sline]->v_flag &= ~VFREQ;
  348.         vtmove(sline, 0);
  349.         if (lp != wp->w_bufp->b_linep) {
  350.             /* if we are not at the end */
  351.             for (i=0; i < llength(lp); ++i)
  352.                 vtputc(lgetc(lp, i));
  353.             lp = lforw(lp);
  354.         }
  355.  
  356.         /* on to the next one */
  357. #if    COLOR
  358.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  359.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  360. #endif
  361.         vteeol();
  362.         ++sline;
  363.     }
  364.  
  365. }
  366.  
  367. /*    updpos:    update the position of the hardware cursor and handle extended
  368.         lines. This is the only update for simple moves.    */
  369.  
  370. updpos()
  371.  
  372. {
  373.     register LINE *lp;
  374.     register int c;
  375.     register int i;
  376.  
  377.     /* find the current row */
  378.     lp = curwp->w_linep;
  379.     currow = curwp->w_toprow;
  380.     while (lp != curwp->w_dotp) {
  381.         ++currow;
  382.         lp = lforw(lp);
  383.     }
  384.  
  385.     /* find the current column */
  386.     curcol = 0;
  387.     i = 0;
  388.     while (i < curwp->w_doto) {
  389.         c = lgetc(lp, i++);
  390.         if (c == '\t')
  391.             curcol |= 0x07;
  392.         else
  393.             if (c < 0x20 || c == 0x7f)
  394.                 ++curcol;
  395.  
  396.         ++curcol;
  397.     }
  398.  
  399.     /* if extended, flag so and update the virtual line image */
  400.     if (curcol >=  term.t_ncol - 1) {
  401.         vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  402.         updext();
  403.     } else
  404.         lbound = 0;
  405. }
  406.  
  407. /*    upddex:    de-extend any line that derserves it        */
  408.  
  409. upddex()
  410.  
  411. {
  412.     register WINDOW *wp;
  413.     register LINE *lp;
  414.     register int i,j;
  415.  
  416.     wp = wheadp;
  417.  
  418.     while (wp != NULL) {
  419.         lp = wp->w_linep;
  420.         i = wp->w_toprow;
  421.  
  422.         while (i < wp->w_toprow + wp->w_ntrows) {
  423.             if (vscreen[i]->v_flag & VFEXT) {
  424.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  425.                    (curcol < term.t_ncol - 1)) {
  426.                     vtmove(i, 0);
  427.                     for (j = 0; j < llength(lp); ++j)
  428.                         vtputc(lgetc(lp, j));
  429.                     vteeol();
  430.  
  431.                     /* this line no longer is extended */
  432.                     vscreen[i]->v_flag &= ~VFEXT;
  433.                     vscreen[i]->v_flag |= VFCHG;
  434.                 }
  435.             }
  436.             lp = lforw(lp);
  437.             ++i;
  438.         }
  439.         /* and onward to the next window */
  440.         wp = wp->w_wndp;
  441.     }
  442. }
  443.  
  444. /*    updgar:    if the screen is garbage, clear the physical screen and
  445.         the virtual screen and force a full update        */
  446.  
  447. updgar()
  448.  
  449. {
  450.     register char *txt;
  451.     register int i,j;
  452.  
  453.     for (i = 0; i < term.t_nrow; ++i) {
  454.         vscreen[i]->v_flag |= VFCHG;
  455. #if    REVSTA
  456.         vscreen[i]->v_flag &= ~VFREV;
  457. #endif
  458. #if    COLOR
  459.         vscreen[i]->v_fcolor = gfcolor;
  460.         vscreen[i]->v_bcolor = gbcolor;
  461. #endif
  462. #if    MEMMAP == 0
  463.         txt = pscreen[i]->v_text;
  464.         for (j = 0; j < term.t_ncol; ++j)
  465.             txt[j] = ' ';
  466. #endif
  467.     }
  468.  
  469.     movecursor(0, 0);         /* Erase the screen. */
  470.     (*term.t_eeop)();
  471.     sgarbf = FALSE;             /* Erase-page clears */
  472.     mpresf = FALSE;             /* the message area. */
  473. #if    COLOR
  474.     mlerase();            /* needs to be cleared if colored */
  475. #endif
  476. }
  477.  
  478. /*    updupd:    update the physical screen from the virtual screen    */
  479.  
  480. updupd(force)
  481.  
  482. int force;    /* forced update flag */
  483.  
  484. {
  485.     register VIDEO *vp1;
  486.     register int i;
  487.  
  488.     for (i = 0; i < term.t_nrow; ++i) {
  489.         vp1 = vscreen[i];
  490.  
  491.         /* for each line that needs to be updated*/
  492.         if ((vp1->v_flag & VFCHG) != 0) {
  493. #if    TYPEAH
  494.             if (force == FALSE && typahead())
  495.                 return(TRUE);
  496. #endif
  497. #if    MEMMAP
  498.             updateline(i, vp1);
  499. #else
  500.             updateline(i, vp1, pscreen[i]);
  501. #endif
  502.         }
  503.     }
  504.     return(TRUE);
  505. }
  506.  
  507. /*    updext: update the extended line which the cursor is currently
  508.         on at a column greater than the terminal width. The line
  509.         will be scrolled right or left to let the user see where
  510.         the cursor is
  511.                                 */
  512.  
  513. updext()
  514.  
  515. {
  516.     register int rcursor;    /* real cursor location */
  517.     register LINE *lp;    /* pointer to current line */
  518.     register int j;        /* index into line */
  519.  
  520.     /* calculate what column the real cursor will end up in */
  521.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz) + term.t_margin;
  522.     taboff = lbound = curcol - rcursor + 1;
  523.  
  524.     /* scan through the line outputing characters to the virtual screen */
  525.     /* once we reach the left edge                    */
  526.     vtmove(currow, -lbound);    /* start scanning offscreen */
  527.     lp = curwp->w_dotp;        /* line to output */
  528.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  529.         vtputc(lgetc(lp, j));
  530.  
  531.     /* truncate the virtual line, restore tab offset */
  532.     vteeol();
  533.     taboff = 0;
  534.  
  535.     /* and put a '$' in column 1 */
  536.     vscreen[currow]->v_text[0] = '$';
  537. }
  538.  
  539. /*
  540.  * Update a single line. This does not know how to use insert or delete
  541.  * character sequences; we are using VT52 functionality. Update the physical
  542.  * row and column variables. It does try an exploit erase to end of line. The
  543.  * RAINBOW version of this routine uses fast video.
  544.  */
  545. #if    MEMMAP
  546. /*    UPDATELINE specific code for the IBM-PC and other compatables */
  547.  
  548. updateline(row, vp1)
  549.  
  550. int row;        /* row of screen to update */
  551. struct VIDEO *vp1;    /* virtual screen image */
  552.  
  553. {
  554. #if    COLOR
  555.     scwrite(row, vp1->v_text, vp1->v_rfcolor, vp1->v_rbcolor);
  556.     vp1->v_fcolor = vp1->v_rfcolor;
  557.     vp1->v_bcolor = vp1->v_rbcolor;
  558. #else
  559.     if (vp1->v_flag & VFREQ)
  560.         scwrite(row, vp1->v_text, 0, 7);
  561.     else
  562.         scwrite(row, vp1->v_text, 7, 0);
  563. #endif
  564.     vp1->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  565.  
  566. }
  567.  
  568. #else
  569.  
  570. updateline(row, vp1, vp2)
  571.  
  572. int row;        /* row of screen to update */
  573. struct VIDEO *vp1;    /* virtual screen image */
  574. struct VIDEO *vp2;    /* physical screen image */
  575.  
  576. {
  577. #if RAINBOW
  578. /*    UPDATELINE specific code for the DEC rainbow 100 micro    */
  579.  
  580.     register char *cp1;
  581.     register char *cp2;
  582.     register int nch;
  583.  
  584.     /* since we don't know how to make the rainbow do this, turn it off */
  585.     flags &= (~VFREV & ~VFREQ);
  586.  
  587.     cp1 = &vp1->v_text[0];                    /* Use fast video. */
  588.     cp2 = &vp2->v_text[0];
  589.     putline(row+1, 1, cp1);
  590.     nch = term.t_ncol;
  591.  
  592.     do
  593.         {
  594.         *cp2 = *cp1;
  595.         ++cp2;
  596.         ++cp1;
  597.         }
  598.     while (--nch);
  599.     *flags &= ~VFCHG;
  600. #else
  601. /*    UPDATELINE code for all other versions        */
  602.  
  603.     register char *cp1;
  604.     register char *cp2;
  605.     register char *cp3;
  606.     register char *cp4;
  607.     register char *cp5;
  608.     register int nbflag;    /* non-blanks to the right flag? */
  609.     int rev;        /* reverse video flag */
  610.     int req;        /* reverse video request flag */
  611.  
  612.  
  613.     /* set up pointers to virtual and physical lines */
  614.     cp1 = &vp1->v_text[0];
  615.     cp2 = &vp2->v_text[0];
  616.  
  617. #if    COLOR
  618.     TTforg(vp1->v_rfcolor);
  619.     TTbacg(vp1->v_rbcolor);
  620. #endif
  621.  
  622. #if    REVSTA | COLOR
  623.     /* if we need to change the reverse video status of the
  624.        current line, we need to re-write the entire line     */
  625.     rev = (vp1->v_flag & VFREV) == VFREV;
  626.     req = (vp1->v_flag & VFREQ) == VFREQ;
  627.     if ((rev != req)
  628. #if    COLOR
  629.         || (vp1->v_fcolor != vp1->v_rfcolor) || (vp1->v_bcolor != vp1->v_rbcolor)
  630. #endif
  631. #if    HP150
  632.     /* the HP150 has some reverse video problems */
  633.         || req || rev
  634. #endif
  635.             ) {
  636.         movecursor(row, 0);    /* Go to start of line. */
  637.         /* set rev video if needed */
  638.         if (rev != req)
  639.             (*term.t_rev)(req);
  640.  
  641.         /* scan through the line and dump it to the screen and
  642.            the virtual screen array                */
  643.         cp3 = &vp1->v_text[term.t_ncol];
  644.         while (cp1 < cp3) {
  645.             TTputc(*cp1);
  646.             ++ttcol;
  647.             *cp2++ = *cp1++;
  648.         }
  649.         /* turn rev video off */
  650.         if (rev != req)
  651.             (*term.t_rev)(FALSE);
  652.  
  653.         /* update the needed flags */
  654.         vp1->v_flag &= ~VFCHG;
  655.         if (req)
  656.             vp1->v_flag |= VFREV;
  657.         else
  658.             vp1->v_flag &= ~VFREV;
  659. #if    COLOR
  660.         vp1->v_fcolor = vp1->v_rfcolor;
  661.         vp1->v_bcolor = vp1->v_rbcolor;
  662. #endif
  663.         return(TRUE);
  664.     }
  665. #endif
  666.  
  667.     /* advance past any common chars at the left */
  668.     while (cp1 != &vp1->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  669.         ++cp1;
  670.         ++cp2;
  671.     }
  672.  
  673. /* This can still happen, even though we only call this routine on changed
  674.  * lines. A hard update is always done when a line splits, a massive
  675.  * change is done, or a buffer is displayed twice. This optimizes out most
  676.  * of the excess updating. A lot of computes are used, but these tend to
  677.  * be hard operations that do a lot of update, so I don't really care.
  678.  */
  679.     /* if both lines are the same, no update needs to be done */
  680.     if (cp1 == &vp1->v_text[term.t_ncol]) {
  681.          vp1->v_flag &= ~VFCHG;        /* flag this line is changed */
  682.         return(TRUE);
  683.     }
  684.  
  685.     /* find out if there is a match on the right */
  686.     nbflag = FALSE;
  687.     cp3 = &vp1->v_text[term.t_ncol];
  688.     cp4 = &vp2->v_text[term.t_ncol];
  689.  
  690.     while (cp3[-1] == cp4[-1]) {
  691.         --cp3;
  692.         --cp4;
  693.         if (cp3[0] != ' ')        /* Note if any nonblank */
  694.             nbflag = TRUE;        /* in right match. */
  695.     }
  696.  
  697.     cp5 = cp3;
  698.  
  699.     /* Erase to EOL ? */
  700.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  701.         while (cp5!=cp1 && cp5[-1]==' ')
  702.             --cp5;
  703.  
  704.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  705.             cp5 = cp3;        /* fewer characters. */
  706.     }
  707.  
  708.     movecursor(row, cp1 - &vp1->v_text[0]);    /* Go to start of line. */
  709. #if    REVSTA
  710.     TTrev(rev);
  711. #endif
  712.  
  713.     while (cp1 != cp5) {        /* Ordinary. */
  714.         TTputc(*cp1);
  715.         ++ttcol;
  716.         *cp2++ = *cp1++;
  717.     }
  718.  
  719.     if (cp5 != cp3) {        /* Erase. */
  720.         TTeeol();
  721.         while (cp1 != cp3)
  722.             *cp2++ = *cp1++;
  723.     }
  724. #if    REVSTA
  725.     TTrev(FALSE);
  726. #endif
  727.     vp1->v_flag &= ~VFCHG;        /* flag this line as updated */
  728.     return(TRUE);
  729. #endif
  730. }
  731. #endif
  732.  
  733. /*
  734.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  735.  * only routine that has any idea of how the modeline is formatted. You can
  736.  * change the modeline format by hacking at this routine. Called by "update"
  737.  * any time there is a dirty window.
  738.  */
  739. modeline(wp)
  740.     WINDOW *wp;
  741. {
  742.     register char *cp;
  743.     register int c;
  744.     register int n;        /* cursor position count */
  745.     register BUFFER *bp;
  746.     register i;            /* loop index */
  747.     register lchar;        /* character to draw line in buffer with */
  748.     register firstm;        /* is this the first mode? */
  749.     char tline[NLINE];        /* buffer for part of mode line */
  750.  
  751.     n = wp->w_toprow+wp->w_ntrows;          /* Location. */
  752.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;/* Redraw next time. */
  753. #if    COLOR
  754.     vscreen[n]->v_rfcolor = 0;            /* black on */
  755.     vscreen[n]->v_rbcolor = 7;            /* white.....*/
  756. #endif
  757.     vtmove(n, 0);                           /* Seek to right line. */
  758.     if (wp == curwp)                /* mark the current buffer */
  759.     lchar = '=';
  760.     else
  761. #if    REVSTA
  762.     if (revexist)
  763.         lchar = ' ';
  764.     else
  765. #endif
  766.         lchar = '-';
  767.  
  768.     vtputc(lchar);
  769.     bp = wp->w_bufp;
  770.  
  771.     if ((bp->b_flag&BFCHG) != 0)                /* "*" if changed. */
  772.         vtputc('*');
  773.     else
  774.         vtputc(lchar);
  775.     n  = 2;
  776.     strcpy(tline, " MicroEMACS ");        /* Buffer name. */
  777.     strcat(tline, VERSION);
  778.     strcat(tline, " (");
  779.  
  780.     /* display the modes */
  781.  
  782.     firstm = TRUE;
  783.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  784.         if (wp->w_bufp->b_mode & (1 << i)) {
  785.             if (firstm != TRUE)
  786.                 strcat(tline, " ");
  787.             firstm = FALSE;
  788.             strcat(tline, modename[i]);
  789.         }
  790.     strcat(tline,") ");
  791.  
  792.     cp = &tline[0];
  793.     while ((c = *cp++) != 0)
  794.         {
  795.         vtputc(c);
  796.         ++n;
  797.         }
  798.  
  799. #if 0
  800.     vtputc(lchar);
  801.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  802.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  803.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  804.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  805.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  806.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  807.     vtputc(lchar);
  808.     n += 8;
  809. #endif
  810.  
  811.     vtputc(lchar);
  812.     vtputc(lchar);
  813.     vtputc(' ');
  814.     n += 3;
  815.     cp = &bp->b_bname[0];
  816.  
  817.     while ((c = *cp++) != 0)
  818.         {
  819.         vtputc(c);
  820.         ++n;
  821.         }
  822.  
  823.     vtputc(' ');
  824.     vtputc(lchar);
  825.     vtputc(lchar);
  826.     n += 3;
  827.  
  828.     if (bp->b_fname[0] != 0)            /* File name. */
  829.         {
  830.     vtputc(' ');
  831.     ++n;
  832.         cp = "File: ";
  833.  
  834.         while ((c = *cp++) != 0)
  835.             {
  836.             vtputc(c);
  837.             ++n;
  838.             }
  839.  
  840.         cp = &bp->b_fname[0];
  841.  
  842.         while ((c = *cp++) != 0)
  843.             {
  844.             vtputc(c);
  845.             ++n;
  846.             }
  847.  
  848.         vtputc(' ');
  849.         ++n;
  850.         }
  851.  
  852.     while (n < term.t_ncol)             /* Pad to full width. */
  853.         {
  854.         vtputc(lchar);
  855.         ++n;
  856.         }
  857. }
  858.  
  859. upmode()    /* update all the mode lines */
  860.  
  861. {
  862.     register WINDOW *wp;
  863.  
  864.     wp = wheadp;
  865.     while (wp != NULL) {
  866.         wp->w_flag |= WFMODE;
  867.         wp = wp->w_wndp;
  868.     }
  869. }
  870.  
  871. /*
  872.  * Send a command to the terminal to move the hardware cursor to row "row"
  873.  * and column "col". The row and column arguments are origin 0. Optimize out
  874.  * random calls. Update "ttrow" and "ttcol".
  875.  */
  876. movecursor(row, col)
  877.     {
  878.     if (row!=ttrow || col!=ttcol)
  879.         {
  880.         ttrow = row;
  881.         ttcol = col;
  882.         TTmove(row, col);
  883.         }
  884.     }
  885.  
  886. /*
  887.  * Erase the message line. This is a special routine because the message line
  888.  * is not considered to be part of the virtual screen. It always works
  889.  * immediately; the terminal buffer is flushed via a call to the flusher.
  890.  */
  891. mlerase()
  892.     {
  893.     int i;
  894.     
  895.     movecursor(term.t_nrow, 0);
  896. #if    COLOR
  897.      TTforg(7);
  898.      TTbacg(0);
  899. #endif
  900.     if (eolexist == TRUE)
  901.         TTeeol();
  902.     else {
  903.         for (i = 0; i < term.t_ncol - 1; i++)
  904.             TTputc(' ');
  905.         movecursor(term.t_nrow, 1);    /* force the move! */
  906.         movecursor(term.t_nrow, 0);
  907.     }
  908.     TTflush();
  909.     mpresf = FALSE;
  910.     }
  911.  
  912. /*
  913.  * Write a message into the message line. Keep track of the physical cursor
  914.  * position. A small class of printf like format items is handled. Assumes the
  915.  * stack grows down; this assumption is made by the "++" in the argument scan
  916.  * loop. Set the "message line" flag TRUE.
  917.  */
  918.  
  919. mlwrite(fmt, arg)
  920.     char *fmt;
  921.     {
  922.     register int c;
  923.     register char *ap;
  924.  
  925. #if    COLOR
  926.     TTforg(7);
  927.     TTbacg(0);
  928. #endif
  929.     if (eolexist == FALSE) {
  930.         mlerase();
  931.         TTflush();
  932.     }
  933.  
  934.     movecursor(term.t_nrow, 0);
  935.     ap = (char *) &arg;
  936.     while ((c = *fmt++) != 0) {
  937.         if (c != '%') {
  938.             TTputc(c);
  939.             ++ttcol;
  940.             }
  941.         else
  942.             {
  943.             c = *fmt++;
  944.             switch (c) {
  945.                 case 'd':
  946.                     mlputi(*(int *)ap, 10);
  947.                     ap += sizeof(int);
  948.                     break;
  949.  
  950.                 case 'o':
  951.                     mlputi(*(int *)ap,  8);
  952.                     ap += sizeof(int);
  953.                     break;
  954.  
  955.                 case 'x':
  956.                     mlputi(*(int *)ap, 16);
  957.                     ap += sizeof(int);
  958.                     break;
  959.  
  960.                 case 'D':
  961.                     mlputli(*(long *)ap, 10);
  962.                     ap += sizeof(long);
  963.                     break;
  964.  
  965.                 case 's':
  966.                     mlputs(*(char **)ap);
  967.                     ap += sizeof(char *);
  968.                     break;
  969.  
  970.         case 'f':
  971.             mlputf(*(int *)ap);
  972.             ap += sizeof(int);
  973.             break;
  974.  
  975.                 default:
  976.                     TTputc(c);
  977.                     ++ttcol;
  978.                 }
  979.             }
  980.         }
  981.     if (eolexist == TRUE)
  982.         TTeeol();
  983.     TTflush();
  984.     mpresf = TRUE;
  985.     }
  986.  
  987. /*
  988.  * Write out a string. Update the physical cursor position. This assumes that
  989.  * the characters in the string all have width "1"; if this is not the case
  990.  * things will get screwed up a little.
  991.  */
  992. mlputs(s)
  993.     char *s;
  994.     {
  995.     register int c;
  996.  
  997.     while ((c = *s++) != 0)
  998.         {
  999.         TTputc(c);
  1000.         ++ttcol;
  1001.         }
  1002.     }
  1003.  
  1004. /*
  1005.  * Write out an integer, in the specified radix. Update the physical cursor
  1006.  * position.
  1007.  */
  1008. mlputi(i, r)
  1009.     {
  1010.     register int q;
  1011.     static char hexdigits[] = "0123456789ABCDEF";
  1012.  
  1013.     if (i < 0)
  1014.         {
  1015.         i = -i;
  1016.         TTputc('-');
  1017.         }
  1018.  
  1019.     q = i/r;
  1020.  
  1021.     if (q != 0)
  1022.         mlputi(q, r);
  1023.  
  1024.     TTputc(hexdigits[i%r]);
  1025.     ++ttcol;
  1026.     }
  1027.  
  1028. /*
  1029.  * do the same except as a long integer.
  1030.  */
  1031. mlputli(l, r)
  1032.     long l;
  1033.     {
  1034.     register long q;
  1035.  
  1036.     if (l < 0)
  1037.         {
  1038.         l = -l;
  1039.         TTputc('-');
  1040.         }
  1041.  
  1042.     q = l/r;
  1043.  
  1044.     if (q != 0)
  1045.         mlputli(q, r);
  1046.  
  1047.     TTputc((int)(l%r)+'0');
  1048.     ++ttcol;
  1049.     }
  1050.  
  1051. /*
  1052.  *    write out a scaled integer with two decimal places
  1053.  */
  1054.  
  1055. mlputf(s)
  1056.  
  1057. int s;    /* scaled integer to output */
  1058.  
  1059. {
  1060.     int i;    /* integer portion of number */
  1061.     int f;    /* fractional portion of number */
  1062.  
  1063.     /* break it up */
  1064.     i = s / 100;
  1065.     f = s % 100;
  1066.  
  1067.     /* send out the integer portion */
  1068.     mlputi(i, 10);
  1069.     TTputc('.');
  1070.     TTputc((f / 10) + '0');
  1071.     TTputc((f % 10) + '0');
  1072.     ttcol += 3;
  1073. }    
  1074.  
  1075. #if RAINBOW
  1076.  
  1077. putline(row, col, buf)
  1078.     int row, col;
  1079.     char buf[];
  1080.     {
  1081.     int n;
  1082.  
  1083.     n = strlen(buf);
  1084.     if (col + n - 1 > term.t_ncol)
  1085.         n = term.t_ncol - col + 1;
  1086.     Put_Data(row, col, n, buf);
  1087.     }
  1088. #endif
  1089.  
  1090.